package com.hopes.bizz.risk.transfer.validation;

import com.hopes.bizz.common.core.util.StringUtils;
import com.hopes.bizz.risk.transfer.constant.RiskRegexpConstant;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * @author lyb
 * @version 1.0
 * @description: UUID校验器
 * @date 2024/2/1 17:05
 */
public class UuidValidator implements ConstraintValidator<UUID, String> {

    private boolean allowEmpty;

    @Override
    public void initialize(UUID uuid) {
        this.allowEmpty = uuid.allowEmpty();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)) {
            return allowEmpty;
        }
        Pattern pattern = Pattern.compile(RiskRegexpConstant.UUID_REGEXP);
        return pattern.matcher(value).matches();
    }
}
